package com.aidl.client

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.os.RemoteException
import android.util.Log
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import sc.ktv.external.youtube.DlLinks
import sc.ktv.external.youtube.IYoutubeDl
import sc.ktv.external.youtube.IYoutubeDlLinksCallback

class MainActivity : AppCompatActivity() {
    companion object {
        const val TAG = "MainActivity"
    }

    private lateinit var btnClick: Button
    private lateinit var tvUrls: TextView

    private var remoteService: IYoutubeDl? = null
    private var connection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            Log.i(TAG, "test aidl client onServiceConnected")
            remoteService = IYoutubeDl.Stub.asInterface(service)
            try {
                remoteService?.registerDlLinksCallback(mCallback);
            } catch (e: RemoteException) {
            }
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            Log.i(TAG, "test aidl client onServiceDisconnected")
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        bindService()

        btnClick = findViewById<Button>(R.id.btn_click)
        tvUrls = findViewById<TextView>(R.id.tv_urls)

        btnClick.setOnClickListener(object : OnClickListener {
            override fun onClick(v: View?) {
                Log.d(TAG, "remoteService:" + remoteService)
                tvUrls.text = ""
                remoteService?.getYoutubeDlLinks("https://www.youtube.com/watch?v=1--pwdu-eJE")
            }
        })
    }

    private fun bindService() {
        val intent = Intent()
        intent.action = "sc.ktv.external.youtube.IYoutubeDl"//子模块中注册时 要求的action
        intent.setPackage("org.beeware.android") //目标应用包名，注意不是子模块的
        val bound = bindService(intent, connection, BIND_AUTO_CREATE)
        if (bound) {
            Log.i(TAG, "test aidl client bindService succeed")
        } else {
            Log.i(TAG, "test aidl client bindService failed")
        }
    }

    private var mCallback: IYoutubeDlLinksCallback.Stub = object : IYoutubeDlLinksCallback.Stub() {
        override fun dlLinks(value: DlLinks?) {
            Log.d(TAG, "urls:" + value?.links)
            runOnUiThread(object : Runnable {
                override fun run() {
                    tvUrls.text = value?.links
                }
            })
        }
    }
}